package org.testis.gui.mvc.utilities;

import org.testis.utils.tree.MyDefaultMutableTreeNode;
import java.io.Serializable;
import java.util.LinkedList;

import org.testis.orm.refinedclasses.AtofComparable;
import org.testis.utils.tree.*;

/**
 *
 * @author emir
 */
public class ComparisonTreeNode extends MyDefaultMutableTreeNode implements Serializable{
    private LinkedList<Operation> m_operations = new LinkedList<Operation>();
    private LinkedList<Operation> m_selectedOperations = new LinkedList<Operation>();
    private MyDefaultMutableTreeNode m_guiReference;
    private static final long serialVersionUID = 20321L;
    
    public ComparisonTreeNode(boolean hasCheckBox) {
        super(hasCheckBox);
    }


    @Override
    public ComparisonTreeNode getParent()
    {
        return (ComparisonTreeNode)super.getParent();
    }
    
    
    public ComparisonTreeNode(MyDefaultMutableTreeNode reference, boolean hasCheckBox) {
        super(reference.getUserObject(),hasCheckBox);
        setGuiReference(reference);
    }

    public LinkedList<Operation> getOperations()
    {
        return m_operations;
    }


    @Override
    public ComparisonTreeNode getChildAt(int n)
    {
        return (ComparisonTreeNode)super.getChildAt(n);
    }


    public void setOperations(LinkedList<Operation> operations)
    {
        m_operations = operations;
    }
    
    public void addOperation(Operation operation) {
        m_operations.add(operation);
    }
    
    public boolean removeOperation(Operation operation) {
        return m_operations.remove(operation);
    }
    
    public boolean addOperationAt(int n, Operation operation) {
        if (n >= m_operations.size()) {
            return false;
        }
        m_operations.add(n, operation);
        return true;
    }
    
    public Operation getOperationAt(int n) {
        return m_operations.get(n);
    }
    
    public void addSelectedOperation(Operation operation) {
        m_selectedOperations.add(operation);
    }
    
    public boolean removeSelectedOperation(Operation operation) {
        return m_selectedOperations.remove(operation);
    }
    
    public boolean addSelectedOperationAt(int n, Operation operation) {
        if (n >= m_selectedOperations.size()) {
            return false;
        }
        m_selectedOperations.add(n, operation);
        return true;
    }
    
    public Operation getSelectedOperationAt(int n) {
        return m_selectedOperations.get(n);
    }


    public MyDefaultMutableTreeNode getGuiReference()
    {
        return m_guiReference;
    }


    public void setGuiReference(MyDefaultMutableTreeNode guiReference)
    {
        m_guiReference = guiReference;
    }
    
    public int compare(ComparisonTreeNode source) {
        AtofComparable c = (AtofComparable)getUserObject();
        int result = c.compare((AtofComparable)source.getUserObject(),null);
        if (result == AtofComparable.EXACT) {
//            source.setColor(ComparisonTreeNode.EXACT);
//            setColor(ComparisonTreeNode.EXACT);
        } 
        else if (result == AtofComparable.DIFFERENT_PARAMETERS) {
            source.setColor(ComparisonTreeNode.DIFF_PARAMETER);
            if (getColor().equals(ComparisonTreeNode.DEFAULT)) {
                setColor(ComparisonTreeNode.DIFF_PARAMETER);
            }
        }
        else if (result == AtofComparable.DIFFERENT_PROPERTIES) {
            source.setColor(ComparisonTreeNode.DIFF_PROPERTY);
            if (getColor().equals(ComparisonTreeNode.DEFAULT)) {
                setColor(ComparisonTreeNode.DIFF_PROPERTY);
            }
        }
        else if (result == AtofComparable.DIFFERENT_TYPE) {
            source.setColor(ComparisonTreeNode.DIFF_TYPE);
            if (getColor().equals(ComparisonTreeNode.DEFAULT)) {
                setColor(ComparisonTreeNode.DIFF_TYPE);
            }
        }
        else if (result == AtofComparable.DIFFERENT_NAME) {
//            source.setColor(ComparisonTreeNode.DIFF_NAME);
//            setColor(ComparisonTreeNode.DIFF_NAME);
        }
        return result;
    }


    public LinkedList<Operation> getSelectedOperations()
    {
        return m_selectedOperations;
    }


    public void setSelectedOperations(LinkedList<Operation> selectedOperations)
    {
        m_selectedOperations = selectedOperations;
    }
}
